home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kjs / ustring.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  13.8 KB  |  477 lines

  1. // -*- c-basic-offset: 2 -*-
  2. /*
  3.  *  This file is part of the KDE libraries
  4.  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  5.  *  Copyright (C) 2003 Apple Computer, Inc.
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public License
  18.  *  along with this library; see the file COPYING.LIB.  If not, write to
  19.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20.  *  Boston, MA 02110-1301, USA.
  21.  *
  22.  */
  23.  
  24. #ifndef _KJS_USTRING_H_
  25. #define _KJS_USTRING_H_
  26.  
  27. #include "global.h"
  28.  
  29. /**
  30.  * @internal
  31.  */
  32. namespace DOM {
  33.   class DOMString;
  34. }
  35. class KJScript;
  36. class QString;
  37. class QConstString;
  38.  
  39. namespace KJS {
  40.  
  41.   class UCharReference;
  42.   class UString;
  43.  
  44.   /**
  45.    * @short Unicode character.
  46.    *
  47.    * UChar represents a 16 bit Unicode character. It's internal data
  48.    * representation is compatible to XChar2b and QChar. It's therefore
  49.    * possible to exchange data with X and Qt with shallow copies.
  50.    */
  51.   struct KJS_EXPORT UChar {
  52.     /**
  53.      * Construct a character with uninitialized value.
  54.      */
  55.     UChar();
  56.     UChar(char u);
  57.     UChar(unsigned char u);
  58.     /**
  59.      * Construct a character with the value denoted by the arguments.
  60.      * @param h higher byte
  61.      * @param l lower byte
  62.      */
  63.     UChar(unsigned char h , unsigned char l);
  64.     /**
  65.      * Construct a character with the given value.
  66.      * @param u 16 bit Unicode value
  67.      */
  68.     UChar(unsigned short u);
  69.     UChar(const UCharReference &c);
  70.     /**
  71.      * @return The higher byte of the character.
  72.      */
  73.     unsigned char high() const { return uc >> 8; }
  74.     /**
  75.      * @return The lower byte of the character.
  76.      */
  77.     unsigned char low() const { return uc; }
  78.     /**
  79.      * @return the 16 bit Unicode value of the character
  80.      */
  81.     unsigned short unicode() const { return uc; }
  82.   public:
  83.     /**
  84.      * @return The character converted to lower case.
  85.      */
  86.     UChar toLower() const;
  87.     /**
  88.      * @return The character converted to upper case.
  89.      */
  90.     UChar toUpper() const;
  91.     /**
  92.      * A static instance of UChar(0).
  93.      */
  94.     static UChar null;
  95.  
  96.     unsigned short uc;
  97.   } KJS_PACKED;
  98.  
  99.   inline UChar::UChar() { }
  100.   inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
  101.   inline UChar::UChar(char u) : uc((unsigned char)u) { }
  102.   inline UChar::UChar(unsigned char u) : uc(u) { }
  103.   inline UChar::UChar(unsigned short u) : uc(u) { }
  104.  
  105.   /**
  106.    * @short Dynamic reference to a string character.
  107.    *
  108.    * UCharReference is the dynamic counterpart of UChar. It's used when
  109.    * characters retrieved via index from a UString are used in an
  110.    * assignment expression (and therefore can't be treated as being const):
  111.    * \code
  112.    * UString s("hello world");
  113.    * s[0] = 'H';
  114.    * \endcode
  115.    *
  116.    * If that sounds confusing your best bet is to simply forget about the
  117.    * existence of this class and treat is as being identical to UChar.
  118.    */
  119.   class KJS_EXPORT UCharReference {
  120.     friend class UString;
  121.     UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
  122.   public:
  123.     /**
  124.      * Set the referenced character to c.
  125.      */
  126.     UCharReference& operator=(UChar c);
  127.     /**
  128.      * Same operator as above except the argument that it takes.
  129.      */
  130.     UCharReference& operator=(char c) { return operator=(UChar(c)); }
  131.     /**
  132.      * @return Unicode value.
  133.      */
  134.     unsigned short unicode() const { return ref().uc; }
  135.     /**
  136.      * @return Lower byte.
  137.      */
  138.     unsigned char low() const { return ref().uc; }
  139.     /**
  140.      * @return Higher byte.
  141.      */
  142.     unsigned char high() const { return ref().uc >> 8; }
  143.     /**
  144.      * @return Character converted to lower case.
  145.      */
  146.     UChar toLower() const { return ref().toLower(); }
  147.     /**
  148.      * @return Character converted to upper case.
  149.      */
  150.     UChar toUpper() const  { return ref().toUpper(); }
  151.   private:
  152.     // not implemented, can only be constructed from UString
  153.     UCharReference();
  154.  
  155.     UChar& ref() const;
  156.     UString *str;
  157.     int offset;
  158.   };
  159.  
  160.   inline UChar::UChar(const UCharReference &c) : uc(c.unicode()) { }
  161.  
  162.   /**
  163.    * @short 8 bit char based string class
  164.    */
  165.   class KJS_EXPORT CString {
  166.   public:
  167.     CString() : data(0L), length(0) { }
  168.     CString(const char *c);
  169.     CString(const char *c, int len);
  170.     CString(const CString &);
  171.  
  172.     ~CString();
  173.  
  174.     CString &append(const CString &);
  175.     CString &operator=(const char *c);
  176.     CString &operator=(const CString &);
  177.     CString &operator+=(const CString &c) { return append(c); }
  178.  
  179.     int size() const { return length; }
  180.     const char *c_str() const { return data; }
  181.   private:
  182.     char *data;
  183.     int length;
  184.   };
  185.  
  186.   /**
  187.    * @short Unicode string class
  188.    */
  189.   class KJS_EXPORT UString {
  190.     friend bool operator==(const UString&, const UString&);
  191.     friend class UCharReference;
  192.     friend class Identifier;
  193.     friend class PropertyMap;
  194.     friend class PropertyMapHashTableEntry;
  195.     /**
  196.      * @internal
  197.      */
  198.     struct KJS_EXPORT Rep {
  199.       friend class UString;
  200.       friend bool operator==(const UString&, const UString&);
  201.  
  202.       static Rep *create(UChar *d, int l);
  203.       void destroy();
  204.  
  205.       UChar *data() const { return dat; }
  206.       int size() const { return len; }
  207.  
  208.       unsigned hash() const { if (_hash == 0) _hash = computeHash(dat, len); return _hash; }
  209.  
  210.       static unsigned computeHash(const UChar *, int length);
  211.       static unsigned computeHash(const char *);
  212.  
  213.       void ref() { ++rc; }
  214.       void deref() { if (--rc == 0) destroy(); }
  215.  
  216.       UChar *dat;
  217.       int len;
  218.       int capacity;
  219.       int rc;
  220.       mutable unsigned _hash;
  221.  
  222.       enum { capacityForIdentifier = 0x10000000 };
  223.  
  224.       static Rep null;
  225.       static Rep empty;
  226.     };
  227.  
  228.   public:
  229.     /**
  230.      * Constructs a null string.
  231.      */
  232.     UString();
  233.     /**
  234.      * Constructs a string from the single character c.
  235.      */
  236.     explicit UString(char c);
  237.     /**
  238.      * Constructs a string from a classical zero determined char string.
  239.      */
  240.     UString(const char *c);
  241.     /**
  242.      * Constructs a string from an array of Unicode characters of the specified
  243.      * length.
  244.      */
  245.     UString(const UChar *c, int length);
  246.     /**
  247.      * If copy is false the string data will be adopted.
  248.      * That means that the data will NOT be copied and the pointer will
  249.      * be deleted when the UString object is modified or destroyed.
  250.      * Behaviour defaults to a deep copy if copy is true.
  251.      */
  252.     UString(UChar *c, int length, bool copy);
  253.     /**
  254.      * Copy constructor. Makes a shallow copy only.
  255.      */
  256.     UString(const UString &s) { attach(s.rep); }
  257.     /**
  258.      * Convenience declaration only ! You'll be on your own to write the
  259.      * implementation for a construction from QString.
  260.      *
  261.      * Note: feel free to contact me if you want to see a dummy header for
  262.      * your favorite FooString class here !
  263.      */
  264.     UString(const QString &);
  265.     /**
  266.      * Convenience declaration only ! See UString(const QString&).
  267.      */
  268.     UString(const DOM::DOMString &);
  269.     /**
  270.      * Concatenation constructor. Makes operator+ more efficient.
  271.      */
  272.     UString(const UString &, const UString &);
  273.     /**
  274.      * Destructor. If this handle was the only one holding a reference to the
  275.      * string the data will be freed.
  276.      */
  277.     ~UString() { release(); }
  278.  
  279.     /**
  280.      * Constructs a string from an int.
  281.      */
  282.     static UString from(int i);
  283.     /**
  284.      * Constructs a string from an unsigned int.
  285.      */
  286.     static UString from(unsigned int u);
  287.     /**
  288.      * Constructs a string from a long.
  289.      */
  290.     static UString from(long l);
  291.     /**
  292.      * Constructs a string from a double.
  293.      */
  294.     static UString from(double d);
  295.  
  296.     /**
  297.      * Append another string.
  298.      */
  299.     UString &append(const UString &);
  300.  
  301.     /**
  302.      * @return The string converted to the 8-bit string type CString().
  303.      */
  304.     CString cstring() const;
  305.     /**
  306.      * Convert the Unicode string to plain ASCII chars chopping of any higher
  307.      * bytes. This method should only be used for *debugging* purposes as it
  308.      * is neither Unicode safe nor free from side effects. In order not to
  309.      * waste any memory the char buffer is static and *shared* by all UString
  310.      * instances.
  311.      */
  312.     char *ascii() const;
  313.     /**
  314.      * @see UString(const QString&).
  315.      */
  316.     DOM::DOMString string() const;
  317.     /**
  318.      * @see UString(const QString&).
  319.      */
  320.     QString qstring() const;
  321.     /**
  322.      * @see UString(const QString&).
  323.      */
  324.     QConstString qconststring() const;
  325.  
  326.     /**
  327.      * Assignment operator.
  328.      */
  329.     UString &operator=(const char *c);
  330.     UString &operator=(const UString &);
  331.     /**
  332.      * Appends the specified string.
  333.      */
  334.     UString &operator+=(const UString &s) { return append(s); }
  335.  
  336.     /**
  337.      * @return A pointer to the internal Unicode data.
  338.      */
  339.     const UChar* data() const { return rep->data(); }
  340.     /**
  341.      * @return True if null.
  342.      */
  343.     bool isNull() const { return (rep == &Rep::null); }
  344.     /**
  345.      * @return True if null or zero length.
  346.      */
  347.     bool isEmpty() const { return (!rep->len); }
  348.     /**
  349.      * Use this if you want to make sure that this string is a plain ASCII
  350.      * string. For example, if you don't want to lose any information when
  351.      * using cstring() or ascii().
  352.      *
  353.      * @return True if the string doesn't contain any non-ASCII characters.
  354.      */
  355.     bool is8Bit() const;
  356.     /**
  357.      * @return The length of the string.
  358.      */
  359.     int size() const { return rep->size(); }
  360.     /**
  361.      * Const character at specified position.
  362.      */
  363.     UChar operator[](int pos) const;
  364.     /**
  365.      * Writable reference to character at specified position.
  366.      */
  367.     UCharReference operator[](int pos);
  368.  
  369.     /**
  370.      * Attempts an conversion to a number. Apart from floating point numbers,
  371.      * the algorithm will recognize hexadecimal representations (as
  372.      * indicated by a 0x or 0X prefix) and +/- Infinity.
  373.      * Returns NaN if the conversion failed.
  374.      * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
  375.      * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
  376.      */
  377.     double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
  378.     double toDouble(bool tolerateTrailingJunk) const;
  379.     double toDouble() const;
  380.     /**
  381.      * Attempts an conversion to an unsigned long integer. ok will be set
  382.      * according to the success.
  383.      @ @param ok make this point to a bool in case you need to know whether the conversion succeeded.
  384.      * @param tolerateEmptyString if false, toULong will return false for *ok for an empty string.
  385.      */
  386.     unsigned long toULong(bool *ok, bool tolerateEmptyString) const;
  387.     unsigned long toULong(bool *ok = 0) const;
  388.  
  389.     unsigned int toUInt32(bool *ok = 0) const;
  390.     unsigned int toStrictUInt32(bool *ok = 0) const;
  391.  
  392.     /**
  393.      * Attempts an conversion to an array index. The "ok" boolean will be set
  394.      * to true if it is a valid array index according to the rule from
  395.      * ECMA 15.2 about what an array index is. It must exactly match the string
  396.      * form of an unsigned integer, and be less than 2^32 - 1.
  397.      */
  398.     unsigned toArrayIndex(bool *ok = 0) const;
  399.  
  400.     /**
  401.      * Returns this string converted to lower case characters
  402.      */
  403.     UString toLower() const;
  404.     /**
  405.      * Returns this string converted to upper case characters
  406.      */
  407.     UString toUpper() const;
  408.     /**
  409.      * @return Position of first occurrence of f starting at position pos.
  410.      * -1 if the search was not successful.
  411.      */
  412.     int find(const UString &f, int pos = 0) const;
  413.     int find(UChar, int pos = 0) const;
  414.     /**
  415.      * @return Position of first occurrence of f searching backwards from
  416.      * position pos.
  417.      * -1 if the search was not successful.
  418.      */
  419.     int rfind(const UString &f, int pos) const;
  420.     int rfind(UChar, int pos) const;
  421.     /**
  422.      * @return The sub string starting at position pos and length len.
  423.      */
  424.     UString substr(int pos = 0, int len = -1) const;
  425.     /**
  426.      * Static instance of a null string.
  427.      */
  428.     static UString null;
  429. #ifdef KJS_DEBUG_MEM
  430.     /**
  431.      * Clear statically allocated resources.
  432.      */
  433.     static void globalClear();
  434. #endif
  435.   private:
  436.     UString(Rep *r) { attach(r); }
  437.     void attach(Rep *r);
  438.     void detach();
  439.     void release();
  440.     Rep *rep;
  441.   };
  442.  
  443.   KJS_EXPORT inline bool operator==(const UChar &c1, const UChar &c2) {
  444.     return (c1.uc == c2.uc);
  445.   }
  446.   KJS_EXPORT inline bool operator!=(const UChar& c1, const UChar& c2) {
  447.     return !KJS::operator==(c1, c2);
  448.   }
  449.   KJS_EXPORT bool operator==(const UString& s1, const UString& s2);
  450.   inline bool operator!=(const UString& s1, const UString& s2) {
  451.     return !KJS::operator==(s1, s2);
  452.   }
  453.   KJS_EXPORT bool operator<(const UString& s1, const UString& s2);
  454.   KJS_EXPORT bool operator==(const UString& s1, const char *s2);
  455.   KJS_EXPORT inline bool operator!=(const UString& s1, const char *s2) {
  456.     return !KJS::operator==(s1, s2);
  457.   }
  458.   KJS_EXPORT inline bool operator==(const char *s1, const UString& s2) {
  459.     return operator==(s2, s1);
  460.   }
  461.   KJS_EXPORT inline bool operator!=(const char *s1, const UString& s2) {
  462.     return !KJS::operator==(s1, s2);
  463.   }
  464.   KJS_EXPORT bool operator==(const CString& s1, const CString& s2);
  465.   KJS_EXPORT inline bool operator!=(const CString& s1, const CString& s2) {
  466.     return !KJS::operator==(s1, s2);
  467.   }
  468.   KJS_EXPORT inline UString operator+(const UString& s1, const UString& s2) {
  469.     return UString(s1, s2);
  470.   }
  471.  
  472.   KJS_EXPORT int compare(const UString &, const UString &);
  473.  
  474. } // namespace
  475.  
  476. #endif
  477.